package com.affaris.controller;


import com.affaris.common.QueryPageParam;
import com.affaris.common.Result;
import com.affaris.controller.dto.UsersPassDTO;
import com.affaris.entity.Apply;
import com.affaris.entity.Menu;
import com.affaris.entity.Trans;
import com.affaris.entity.Users;
import com.affaris.service.MenuService;
import com.affaris.service.UsersService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lucas
 * @since 2023-05-15
 */
@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;
    @Autowired
    private MenuService menuService;


    @GetMapping("/list")
    public List<Users> list() {
        return usersService.list();
    }

    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Users users) {
        return usersService.save(users) ? Result.suc() : Result.fail();
    }

    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody Users users) {
        return usersService.updateById(users);
    }

    //新增或修改
    @PostMapping("/saveOrMod")
    public boolean saveOrMod(@RequestBody Users users) {
        return usersService.saveOrUpdate(users);
    }

    //删除
    @GetMapping("/delete")
    public Result delete(@RequestParam String id) {
        return usersService.removeById(id) ? Result.suc() : Result.fail();
    }

    //查询（模糊、匹配）
    @PostMapping("/query")
    public Result query(@RequestBody Users users) {
        LambdaQueryWrapper<Users> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotBlank(users.getName())) {
            lambdaQueryWrapper.like(Users::getName, users.getName());
        }
        return Result.suc(usersService.list(lambdaQueryWrapper));
    }

    //分页
    @PostMapping("/page")
    public Result page(@RequestBody QueryPageParam queryPageParam) {
        HashMap param = queryPageParam.getParam();
        String name = (String) param.get("name");
        String userIdentity = (String) param.get("userIdentity");
        List<String> userIdentityList = (List<String>) param.get("userIdentityList");
        String organization = (String) param.get("organization");

        System.out.println("num==" + queryPageParam.getPageNum());
        System.out.println("size==" + queryPageParam.getPageSize());

        Page<Users> page = new Page();
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());

        LambdaQueryWrapper<Users> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotBlank(name) && !"null".equals(name)) {
            lambdaQueryWrapper.like(Users::getName, name);
        }
        if (StringUtils.isNotBlank(userIdentity)) {
            lambdaQueryWrapper.eq(Users::getUserIdentity, userIdentity);
        }
        if (userIdentityList != null && userIdentityList.size() > 0) { // 判断userIdentityList是否为空
            lambdaQueryWrapper.in(Users::getUserIdentity, userIdentityList); // 使用in关键字查询
        }
        if (StringUtils.isNotBlank(organization)) {
            lambdaQueryWrapper.eq(Users::getOrganization, organization);
        }

        IPage result = usersService.page(page, lambdaQueryWrapper);

        System.out.println("result===" + result.getTotal());

        return Result.suc(result.getRecords(), result.getTotal());
    }

    //账号唯一验证
    @GetMapping("/findByStudentId")
    public Result findByStudentId(@RequestParam String studentId) {
        List list = usersService.lambdaQuery().eq(Users::getStudentId, studentId).list();
        return list.size() > 0 ? Result.suc(list) : Result.fail();
    }

    //更新
    @PostMapping("/update")
    public Result update(@RequestBody Users users) {
        return usersService.updateById(users) ? Result.suc() : Result.fail();
    }

    //更新密码
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody UsersPassDTO usersPassDTO) {
        return usersService.updatePassword(usersPassDTO) ? Result.suc() : Result.fail();
    }

    //验证登录密码
    @PostMapping("/login")
    public Result login(@RequestBody Users users) {
        List list = usersService.lambdaQuery()
                .eq(Users::getStudentId, users.getStudentId())
                .eq(Users::getPassword, users.getPassword())
                .list();

        if (list.size() > 0) {
            Users users1 = (Users) list.get(0);
            List menuList = menuService.lambdaQuery()
                    .like(Menu::getMenuRight, users1.getUserIdentity())
                    .list();

            HashMap res = new HashMap();
            res.put("users", users1);
            res.put("menu", menuList);
            return Result.suc(res);
        }
        return Result.fail();
    }
}